package com.leyou.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;

import com.leyou.common.vo.PageResult;
import com.leyou.domain.Role;
import com.leyou.domain.UserManage;
import com.leyou.dto.RoleDTO;
import com.leyou.dto.UserManageDTO;
import com.leyou.mapper.UserManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserManageService {

    @Autowired
    private UserManageMapper userManageMapper;

    /**
     * 分页排序 搜索查询
     * @param page 当前页
     * @param rows 每页的行数
     * @return
     */
    public PageResult<UserManageDTO> queryUserManageByPage(Integer page, Integer rows) {
        //分页
        PageHelper.startPage(page,rows);
        //查询用户
        List<UserManage> users = userManageMapper.findAll();
        //解析分页结果
        PageInfo<UserManage> pageInfo = new PageInfo<>(users);
        //将对象转换成DTO
        List<UserManageDTO> userList = BeanHelper.copyWithCollection(users, UserManageDTO.class);
        for (UserManageDTO user : userList) {
            List<RoleDTO> roles = user.getRoles();
            List<RoleDTO> roleDTOS = BeanHelper.copyWithCollection(roles, RoleDTO.class);
            user.setRoles(roleDTOS);
        }
        //调用showRoleName方法将rolename字符串存到UsermanageDTO对象中；
        showRoleName(userList);

        //返回
        return new PageResult<>(pageInfo.getTotal(),userList);
    }

    //得到rolename字符串
    private void showRoleName(List<UserManageDTO> userList){
        for (UserManageDTO userManageDTO : userList){
            String rolename = userManageDTO.getRoles().stream().map(RoleDTO::getRoleName).collect(Collectors.joining("——"));
            userManageDTO.setRolename(rolename);
        }
    }

    /**
     * 增添用户信息
     * @param userManageDTO
     */
    public void addUserManage(UserManageDTO userManageDTO) {
        //将DTO转成pojo格式
        UserManage user = BeanHelper.copyProperties(userManageDTO, UserManage.class);
        List<Role> roleDTO = user.getRoles();
        List<Role> roles = BeanHelper.copyWithCollection(roleDTO, Role.class);
        user.setRoles(roles);
        String rolename = roles.stream().map(Role::getRoleName).collect(Collectors.joining("——"));
        user.setRolename(rolename);
        //得到rids
        List<Long> rids = user.getRoles().stream().map(Role::getId).collect(Collectors.toList());
        //增添用户信息
        int i = userManageMapper.insertSelective(user);
        //判断是否增添用户成功
        if(i != 1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //增添用户和角色的中间表信息
        i = userManageMapper.insertUserRoles(user.getId(), rids);
        //判断中间表是否插入成功
        if(i != rids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    //修改用户信息
    public void updateUserManage(UserManageDTO userManageDTO) {
        //将DTO装成pojo对象
        UserManage user = BeanHelper.copyProperties(userManageDTO, UserManage.class);
        List<Role> rolesDTO = user.getRoles();
        List<Role> roles = BeanHelper.copyWithCollection(rolesDTO, Role.class);
        user.setRoles(roles);
        String rolename = roles.stream().map(Role::getRoleName).collect(Collectors.joining("——"));
        user.setRolename(rolename);
        //得到rids
        List<Long> rids = user.getRoles().stream().map(Role::getId).collect(Collectors.toList());
        //修改用户信息
        int i = userManageMapper.updateByPrimaryKeySelective(user);
        if (1 != 1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
        //修改中间表（先删除再修改）
        //删除中间表信息
        userManageMapper.deleteUserRole(user.getId());
        //增添中间表
        i = userManageMapper.insertUserRoles(user.getId(),rids);
        //判断中间表是否插入成功
        if (i != rids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    //根据id查找用户信息
    public UserManageDTO queryUserManageById(Long id) {
        UserManage userManage = userManageMapper.selectByPrimaryKey(id);
        UserManageDTO userManageDTO = BeanHelper.copyProperties(userManage, UserManageDTO.class);
        return userManageDTO;
    }

    //删除用户信息
    public void deleteUserManage(Long id) {
        UserManage userManage = new UserManage();
        userManage.setId(id);
        //删除用户信息
        int i = userManageMapper.delete(userManage);
        //判断是否删除成功
        if (i != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
        //删除中间表
        userManageMapper.deleteUserRole(id);
    }
}
